package cn.tedu.hospital.admin.server.patient.dao.persist.repository.impl;

import cn.tedu.hospital.admin.server.common.pojo.vo.PageData;
import cn.tedu.hospital.admin.server.common.util.PageInfoToPageDataConverter;
import cn.tedu.hospital.admin.server.patient.dao.persist.mapper.PatientMapper;
import cn.tedu.hospital.admin.server.patient.dao.persist.repository.IPatientRepository;
import cn.tedu.hospital.admin.server.patient.pojo.entity.Patient;
import cn.tedu.hospital.admin.server.patient.pojo.vo.PatientListItemVO;
import cn.tedu.hospital.admin.server.patient.pojo.vo.PatientStandardVO;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * 处理患者数据的存储库实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@Repository
public class PatientRepositoryImpl implements IPatientRepository {

    @Autowired
    private PatientMapper patientMapper;


    @Override
    public int insert(Patient patient) {
        log.debug("开始执行【插入患者】的数据访问，参数：{}", patient);
        return patientMapper.insert(patient);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据Id删除患者】的数据访问，参数：{}", id);
        return patientMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        log.debug("开始执行【批量删除患者】的数据访问，参数：{}", idList);
        return patientMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Patient patient) {
        log.debug("开始执行【更新患者】的数据访问，参数：{}", patient);
        return patientMapper.updateById(patient);
    }

    @Override
    public int count() {
        log.debug("开始执行【统计患者的数量】的数据访问，参数：无");
        return patientMapper.selectCount(null);
    }

    @Override
    public int countByPatientId(String patientId) {
        log.debug("开始执行【根据名称\"{}\"统计患者表中数据的数量】，参数：", patientId);
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patientId", patientId);
        return patientMapper.selectCount(queryWrapper);
    }

    @Override
    public PatientStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询患者信息】的数据访问，参数：{}", id);
        return patientMapper.getStandardById(id);
    }

    @Override
    public PageData<PatientListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询患者列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<PatientListItemVO> list = patientMapper.list();
        PageInfo<PatientListItemVO> pageInfo = new PageInfo<>(list);
        PageData<PatientListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }



}